package com.hnkywl.sts.action.zjgl;

import com.hnkywl.sts.entity.organ.Nbgs;
import com.hnkywl.sts.entity.xsgl.Kcbj;
import com.hnkywl.sts.entity.zjgl.Fycb;
import com.hnkywl.sts.service.organ.NbgsService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.service.zjgl.FycbService;
import com.hnkywl.sts.util.POIUtil;
import jxl.Sheet;
import jxl.Workbook;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.Region;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-11
 * Time: 下午2:23
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/zjgl/fycb")
public class FycbAaction extends SimpleActionSupport {
    private File upload;

    private FycbService fycbService;

    private Fycb fycb;

    private int page;
    //分页组件 页面大小
    private int rows = 5;


    private String fycbnf;

    private String fycbyf;

    private YgService ygService;

    private Date drsj;

    private NbgsService nbgsService;

    //公司ID
    private Long gsId;
    //公司名称
    private String gsMc;

    @Action("fycb")
    public String fycb() {
        Calendar cal = Calendar.getInstance();
        fycbnf = cal.get(cal.YEAR)+"";
        fycbyf = cal.get(cal.MONTH)+"";
        return SUCCESS;
    }

    @Action("search_fycb")
    public String searchfycb() {
        return SUCCESS;
    }



    @Action("fycbList")
    public void fycbList() {
        Calendar cal = Calendar.getInstance();
        if(fycbnf==null || ("").equals(fycbnf)){
            fycbnf = cal.get(cal.YEAR)+"";
        }
        if(fycbyf==null || ("").equals(fycbyf)){
            fycbyf = cal.get(cal.MONTH)+"";
        }
        this.renderJson(this.fycbService.findAllfycb(fycbnf,fycbyf, gsId, gsMc,page, rows));
    }



    //科目代码导入界面展示
    @Action("fycb_upload")
    public String fycbUpload() {
        return SUCCESS;
    }

    //库存报价导出
    @Action("fycb_export")
    public void kcbjExport(){
        SimpleDateFormat dateformat2=new SimpleDateFormat("yyyy-MM");
        List<Nbgs> nbgsList = this.nbgsService.findAllNbgsList();
        //第一步，创建一个webbook，对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
        HSSFSheet sheet = wb.createSheet("费用成本");
        POIUtil poiUtil = new POIUtil(wb);
        //设置列宽
        sheet.setDefaultRowHeightInPoints(15);
        sheet.setDefaultColumnWidth((short)9);
        sheet.setColumnWidth((short) 0,(short) (8*512));
        sheet.setColumnWidth((short) 1,(short) (8*512));
        sheet.setColumnWidth((short) 2,(short) (8*512));
        sheet.setColumnWidth((short) 3,(short) (8*512));
        sheet.setColumnWidth((short) 4,(short) (8*512));
        sheet.setColumnWidth((short) 5,(short) (8*512));
        sheet.setColumnWidth((short) 6,(short) (8*512));
        sheet.setColumnWidth((short) 7,(short) (8*512));
        sheet.setColumnWidth((short) 8,(short) (8*512));
        sheet.setColumnWidth((short) 9,(short) (8*512));
        sheet.setColumnWidth((short) 10,(short) (8*512));
        sheet.setColumnWidth((short) 11,(short) (8*512));
        //第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
        HSSFRow row = sheet.createRow((int)0);
        row.setHeightInPoints(36);
        //第四步，创建单元格，并设置值表头  设置表头居中
        HSSFCellStyle titleStyle = poiUtil.createTitleHSSFCellStyle(wb);
        HSSFCell titleCell = row.createCell((short)0); titleCell.setCellStyle(titleStyle);
        titleCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        titleCell.setCellValue("费用成本导出汇总表");
        sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 8));
        row = sheet.createRow((int)1);
        row.setHeightInPoints(21);
        HSSFCell deptCell = row.createCell((short)0);deptCell.setCellStyle(poiUtil.createMonthHSSFCellStyle(wb));
        deptCell.setEncoding(HSSFCell.ENCODING_UTF_16);
        deptCell.setCellValue("填制日期:");
        HSSFCell dqrq = row.createCell((short)1);dqrq.setCellStyle(poiUtil.createMonthHSSFCellStyle(wb));
        dqrq.setEncoding(HSSFCell.ENCODING_UTF_16);
        dqrq.setCellValue(dateformat2.format(new Date()));
        row = sheet.createRow(2);
        row.setHeightInPoints(20);

        //生成第一行数据
        HSSFCell compId = row.createCell((short)0);compId.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        compId.setEncoding(HSSFCell.ENCODING_UTF_16);
        compId.setCellValue("公司Id");
        HSSFCell compMc = row.createCell((short)1);compMc.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        compMc.setEncoding(HSSFCell.ENCODING_UTF_16);
        compMc.setCellValue("公司名称");
        HSSFCell yhr = row.createCell((short)2);yhr.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        yhr.setEncoding(HSSFCell.ENCODING_UTF_16);
        yhr.setCellValue("印花税");
        HSSFCell yysj = row.createCell((short)3);yysj.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        yysj.setEncoding(HSSFCell.ENCODING_UTF_16);
        yysj.setCellValue("营业税金及金额");
        HSSFCell qtrf = row.createCell((short)4);qtrf.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        qtrf.setEncoding(HSSFCell.ENCODING_UTF_16);
        qtrf.setCellValue("其他税费");
        HSSFCell rfSum = row.createCell((short)5);rfSum.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        rfSum.setEncoding(HSSFCell.ENCODING_UTF_16);
        rfSum.setCellValue("税费小计");
        HSSFCell kkfy = row.createCell((short)6);kkfy.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        kkfy.setEncoding(HSSFCell.ENCODING_UTF_16);
        kkfy.setCellValue("可控费用");
        HSSFCell gdfy = row.createCell((short)7);gdfy.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        gdfy.setEncoding(HSSFCell.ENCODING_UTF_16);
        gdfy.setCellValue("固定费用");
        HSSFCell glfySum = row.createCell((short)8);glfySum.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        glfySum.setEncoding(HSSFCell.ENCODING_UTF_16);
        glfySum.setCellValue("管理费用小计");
        HSSFCell gzfj = row.createCell((short)9);gzfj.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        gzfj.setEncoding(HSSFCell.ENCODING_UTF_16);
        gzfj.setCellValue("工资及附加");
        HSSFCell lwpq = row.createCell((short)10);lwpq.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        lwpq.setEncoding(HSSFCell.ENCODING_UTF_16);
        lwpq.setCellValue("劳务派遣");
        HSSFCell rlzySum = row.createCell((short)11);rlzySum.setCellStyle(poiUtil.createHeaderHSSFCellStyle(wb));
        rlzySum.setEncoding(HSSFCell.ENCODING_UTF_16);
        rlzySum.setCellValue("人力资源成本小计");
        //表体部分
        for (int i=3;i<nbgsList.size()+3;i++) {
            Nbgs n = nbgsList.get(i-3);
            row = sheet.createRow(i);row.setHeightInPoints(20);
            compId = row.createCell((short)0);compId.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            compId.setEncoding(HSSFCell.ENCODING_UTF_16);
            compId.setCellValue(n.getId());
            compMc = row.createCell((short)1);compMc.setCellStyle(poiUtil.createContentHSSFCellStyle(wb));
            compMc.setEncoding(HSSFCell.ENCODING_UTF_16);
            compMc.setCellValue(n.getJc());
        }
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String((dateformat2.format(new Date())+"费用汇总表.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败"+e.getMessage());
        }
    }

    //费用成本
    @Action("fycb_import")
    public void fycbImport() {
        try {
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM");
            SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyyMM");
            Workbook book = Workbook.getWorkbook(upload);
            //  获得第一个工作表对象
            Sheet sheet = book.getSheet(0);
            this.fycbService.deleteFycbBySj(dateformat1.format(drsj));
            //获取总行数
            int rows = sheet.getRows();
            //获取总列数
            int columns = sheet.getColumns();
            for (int i = 3; i < rows; i++) {
                Fycb temp = new Fycb();
                temp.setMonth(Integer.parseInt(dateformat1.format(drsj)));
                String cell1 = sheet.getCell(0, i).getContents().trim();
                temp.setGs(nbgsService.findNbgsDetail(Long.parseLong(cell1)));
                String cell3 = sheet.getCell(2, i).getContents().trim();
                temp.setYhs(Float.parseFloat(cell3));
                String cell4 = sheet.getCell(3, i).getContents().trim();
                temp.setYysj(Float.parseFloat(cell4));
                String cell5 = sheet.getCell(4, i).getContents().trim();
                temp.setQtrf(Float.parseFloat(cell5));
                String cell6 = sheet.getCell(5, i).getContents().trim();
                temp.setRfxj(Float.parseFloat(cell6));
                String cell7 = sheet.getCell(6, i).getContents().trim();
                temp.setKkfy(Float.parseFloat(cell7));
                String cell8 = sheet.getCell(7, i).getContents().trim();
                temp.setGdfy(Float.parseFloat(cell8));
                String cell9 = sheet.getCell(8, i).getContents().trim();
                temp.setGlfyxj(Float.parseFloat(cell9));
                String cell10 = sheet.getCell(9, i).getContents().trim();
                temp.setGzfj(Float.parseFloat(cell10));
                String cell11 = sheet.getCell(10, i).getContents().trim();
                temp.setLwpq(Float.parseFloat(cell11));
                String cell12 = sheet.getCell(11, i).getContents().trim();
                temp.setRlzyxj(Float.parseFloat(cell12));
                this.fycbService.saveFycb(temp);
            }
            renderText("费用成本导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            renderText(e.getMessage().toString());
        }
    }



    //=======================GetterAndSetter===========================================================

    public File getUpload() {
        return upload;
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public Fycb getFycb() {
        return fycb;
    }

    public void setFycb(Fycb fycb) {
        this.fycb = fycb;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getFycbnf() {
        return fycbnf;
    }

    public void setFycbnf(String fycbnf) {
        this.fycbnf = fycbnf;
    }

    public String getFycbyf() {
        return fycbyf;
    }

    public void setFycbyf(String fycbyf) {
        this.fycbyf = fycbyf;
    }

    public Date getDrsj() {
        return drsj;
    }

    public void setDrsj(Date drsj) {
        this.drsj = drsj;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public String getGsMc() {
        return gsMc;
    }

    public void setGsMc(String gsMc) {
        this.gsMc = gsMc;
    }

    @Autowired
    public void setFycbService(FycbService fycbService) {
        this.fycbService = fycbService;
    }

    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }
    @Autowired
    public void setNbgsService(NbgsService nbgsService) {
        this.nbgsService = nbgsService;
    }
}
